package com.ljpz.search.controller;

import com.ljpz.search.bean.Item;
import com.ljpz.search.bean.SearchResult;
import com.ljpz.search.service.ItemSearchService;
import org.apache.solr.client.solrj.SolrServerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * Created by liujunchen on 2016/12/27.
 */
@Controller
public class SearchController {
    public  static  final  Integer ROWS=32 ;
    @Autowired
    private ItemSearchService itemSearchService;
    @RequestMapping(value = "search",method = RequestMethod.GET)
    public ModelAndView search(@RequestParam("q")String keywords, @RequestParam(value = "page",defaultValue = "1")Integer  page){
        ModelAndView  mv = new ModelAndView("search")  ;

        //解决乱码问题
        try {
            keywords=new String(keywords.getBytes("ISO-8859-1"),"UTF-8")  ;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            keywords=""   ;
        }
        //搜索关键字
        mv.addObject("query",keywords)  ;

        SearchResult searchResult = null;
        try {
            searchResult = this.itemSearchService.search(keywords,page,ROWS);
        } catch (SolrServerException e) {
            e.printStackTrace();
            searchResult= new SearchResult(0L,null) ;
        }

        //搜索结果数据
        mv.addObject("itemList",searchResult.getList());
        //页数
        mv.addObject("page",page)  ;
        int  total = searchResult.getTotal().intValue() ;
        int pages= total%ROWS==0?total/ROWS:total/ROWS+1;
        //总页数
        mv.addObject("pages",pages)   ;

        return  mv ;

    }
}
